using System;
using System.Collections.Generic;
using System.Text;
using KbWorksBlogMigrationtool.wss2007;
using System.Data;
using System.Xml;
using System.Net;
using System.Configuration;
namespace KbWorksBlogMigrationtool
{
    class ListItemsProxy2007
    {
        AppSettingsReader conf = new AppSettingsReader();
        private string url = string.Empty;
        private string listName = string.Empty;
        Logging log = new Logging();
        public ListItemsProxy2007(string Url, string ListName)
        {
            if (Url == String.Empty)
            {
                throw new ArgumentException("Parameter may not be String.Empty", "Url");
            }

            if (ListName == String.Empty)
            {
                throw new ArgumentException("Parameter may not be String.Empty", "ListName");
            }

            url = Url + "/_vti_bin/Lists.asmx";
            listName = ListName;
        }

        public XmlNode GetListItems()
        {

            KbWorksBlogMigrationtool.wss2007.Lists ListItems = new KbWorksBlogMigrationtool.wss2007.Lists();
            ListItems.Url = url;
            ListItems.Credentials =
                new NetworkCredential(Convert.ToString(conf.GetValue("networkuser", typeof(string))),
                 Convert.ToString(conf.GetValue("networkpass", typeof(string))),
                 Convert.ToString(conf.GetValue("domain", typeof(string))));

            XmlDocument doc = new XmlDocument();

            //Get the data from the list
            XmlNode listItemsCollection = null;


            XmlElement listQuery = doc.CreateElement("Query");
            XmlElement OrderBy = doc.CreateElement("OrderBy");
            XmlElement CreatedFieldRef = doc.CreateElement("FieldRef");
            XmlAttribute FieldRefNameAttrib = doc.CreateAttribute("Name");
            FieldRefNameAttrib.Value = "Created";
            XmlAttribute FieldRefAscendingAttrib = doc.CreateAttribute("Ascending");
            FieldRefAscendingAttrib.Value = "TRUE";
            CreatedFieldRef.Attributes.Append(FieldRefNameAttrib);
            CreatedFieldRef.Attributes.Append(FieldRefAscendingAttrib);
            OrderBy.AppendChild(CreatedFieldRef);
            listQuery.AppendChild(OrderBy);

            XmlElement listViewFields = doc.CreateElement("ViewFields");

            XmlElement listQueryOptions = doc.CreateElement("QueryOptions");

            listQueryOptions.InnerXml = "";

            try
            {
                listItemsCollection = ListItems.GetListItems(listName, string.Empty, listQuery, listViewFields, "9999", listQueryOptions,null);
                 //   ListItems.GetListItems(listName, String.Empty, listQuery, listViewFields, 
                   //     "9999", listQueryOptions);
            }
            catch (Exception ex)
            {
                log.append("Unable to get ListItems on " + url + " " + ex.ToString(), 4, "Error");
            }
            finally
            {
                ListItems.Dispose();
            }
            return listItemsCollection;
          

        }
        public XmlNode GetAttachments(string  listName,string  ID)
        {

            KbWorksBlogMigrationtool.wss2007.Lists ListAtt = new KbWorksBlogMigrationtool.wss2007.Lists();
            ListAtt.Url = url;
            ListAtt.Credentials =
                new NetworkCredential(Convert.ToString(conf.GetValue("networkuser", typeof(string))),
                 Convert.ToString(conf.GetValue("networkpass", typeof(string))),
                 Convert.ToString(conf.GetValue("domain", typeof(string))));

            XmlDocument doc = new XmlDocument();
             XmlNode DocumentAttachments = null;
            try{
                DocumentAttachments=ListAtt.GetAttachmentCollection(listName, ID);
                
                //get the items

                return DocumentAttachments;

                        }
                
            catch (Exception ex)
            {
                log.append("Unable to get Attachents on " + url + "  " +listName+ "  " + ex.ToString(), 4, "Error");
                 return null;
            }
            return DocumentAttachments;

        }
    }
}
